package cn.biplam.setup.code;

import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.front.callback.AnchorCallBack;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Clear_Type;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.template.base.Button;
import cn.biplam.front.template.base.Input;
import cn.biplam.front.template.base.Select;
import cn.biplam.front.template.base.SimpleInput;
import cn.biplam.front.template.base.SimpleTextArea;
import cn.biplam.front.template.control.FlatTable;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.control.table.TablePropertys.Col_Content_Type;
import cn.biplam.front.template.control.table.TableScript.Action_Type;
import cn.biplam.setup.code.product.ProductData;

public class CodeManageBean {

	/**
	 * 获取编辑界面
	 * @param mUser
	 * @param sr
	 * @return
	 * @throws ErrorMessage
	 */
	public HtmlWindow getPanel(UserInfo mUser,SqlResult sr,boolean edit,String codeId,
			ExchangeCodeData codeData,String codeEditTableId,String recordId,String sHomeUrl) throws ErrorMessage{

		String codeName=null;
		String codeClassPath=null;
		String codeDesc=null;
		String productId = null;

		if(TOOLS.exist(sr)){
			sr.first();
			sr.next();
			codeId = sr.getString(codeData.CODE_ID);
			codeName=sr.getString(codeData.CODE_NAME);
			codeClassPath=sr.getString(codeData.CODE_CLASS_PATH);
			codeDesc=sr.getString(codeData.CODE_DESC);
			productId = sr.getString(codeData.PRODUCT_ID);
		}

		FlatTable table = new FlatTable(codeEditTableId,recordId);
		table.putTh("代码编号",0,0);
		table.putTd(new SimpleInput(codeData.CODE_ID,"15",codeId,edit).get(), 0, 1);
		table.putTh("代码名称",1,0);
		table.putTd(new SimpleInput(codeData.CODE_NAME,"50",codeName).get(), 1, 1);
		table.putTh("所属产品",2,0);
		table.putTd(getProductSelect(codeData, sHomeUrl,productId), 2, 1);
		table.putTh("代码类路径",3,0);
		table.putTd(new SimpleTextArea(codeData.CODE_CLASS_PATH,"48","3",codeClassPath).get(), 3, 1);
		table.putTh("代码描述",4,0);
		table.putTd(new SimpleTextArea(codeData.CODE_DESC,"48",codeDesc).get(), 4, 1);
		table.setColSize(0, 10);
		HtmlWindow win = table.get();
		win.setStyleSize(400, 190);
		return win;
	}

	/**
	 * 获取代码内容
	 * @return
	 * @throws ErrorMessage
	 */
	public HtmlWindow getCodeContent(String tablePageID, SqlResult codes,
			ExchangeCodeData codeData,TablePropertys property) throws ErrorMessage{
		String mainKey = codeData.CODE_ID;
		/** 创建表对象 */
		FlowTable st=new FlowTable(tablePageID,mainKey,property); 
		OperatorResult sr = new OperatorResult();
		sr.setSelectResult(codes);
		/** 将数据集放入表格 */
		st.put(sr);
		st.setHeight("500");
		HtmlWindow win = st.get();
		return win;
	}

	/**
	 * 获取表格属性
	 * @return
	 * @throws ErrorMessage
	 */
	public TablePropertys getProperty(String sHomeUrl, UserInfo mUser,
			String tablePageID,ExchangeCodeData codeData,
			String refreshAct,String editAct,String delAct,String targId) throws ErrorMessage{
		TablePropertys property = new TablePropertys(sHomeUrl, mUser);
		property.putCol(codeData.CODE_ID, "代码");
		property.putCol(codeData.CODE_NAME,Col_Content_Type.NAME_ANCHOR,
				"代码名称",new AnchorCallBack(codeData.CODE_NAME,"查看下级"));
		property.putCol(codeData.CODE_CLASS_PATH,Col_Content_Type.DESC_LONG_TEXT, "代码类路径");
		property.putCol(codeData.CODE_DESC,Col_Content_Type.DESC_LONG_TEXT, "代码描述");
		property.putCol("operator", Col_Content_Type.BUTTON, "", new CodeMangeCallback());
		property.setColAction(codeData.CODE_NAME,
				Action_Type.NONE_POPE_M, refreshAct, targId);
		property.setColAction("operator", Action_Type.POP_SUBMIT_PR, editAct);
		property.setColAction("operator", Action_Type.POP_SUBMIT_PR, delAct);
		return property;
	}

	/**
	 * 获取产品选择界面选择
	 * @param codeData
	 * @param sHomeUrl
	 * @return
	 * @throws ErrorMessage
	 */
	public HtmlWindow getProductSelect(ExchangeCodeData codeData,String sHomeUrl,
			String productId) throws ErrorMessage{
		ProductData product = new ProductData();
		SqlResult rs = product.getAll();
		Select sel = null;
		sel = new Select(codeData.PRODUCT_ID, productId, sHomeUrl);
		sel.set(rs, ProductData.PRODUCT_ID, ProductData.PRODUCT_NAME);
		return sel.get();
	}

	/**
	 * 查找交换代码
	 * @return
	 * @throws ErrorMessage 
	 */
	public HtmlWindow searchExchangeClass(ExchangeCodeData codeData,String script) throws ErrorMessage{
		HtmlWindow div = new HtmlDiv();
		div.put(new Input("代码",codeData.CODE_ID).get());
		div.put(new Input("代码名称",codeData.CODE_NAME).get());
		div.put(new Input("代码类",codeData.CODE_CLASS_PATH).get());
		Button bt = new Button(null,"查找",script);
		div.put(bt.get());
		div.setMarginTop("3");
		div.setClear(Clear_Type.Both);
		div.setFloat(Float_Type.Left);
		div.setStyleSize(620, 22);
		return div;
	}
}
